<?php
/**
 * Created by PhpStorm.
 * User: boye009
 * Date: 2016/4/7
 * Time: 15:31
 */

namespace app\shop\api;

use app\common\api\BaseApi;
use app\shop\model\ProductProp;

class ProductPropApi extends BaseApi{

    const ADD             = "shop/ProductPropApi/add";
    const SAVE            = "shop/ProductPropApi/save";
    const GET_INFO        = "shop/ProductPropApi/getInfo";
    const QUERY_NO_PAGING = "shop/ProductPropApi/queryNoPaging";
    const GET_PROP_BY_PID = "shop/ProductPropApi/getPropByPid";
    const GET_PROP2       = "shop/ProductPropApi/getProp2";

    protected function _init(){
        $this -> model = new ProductProp();
    }

    public function getProp2($pid){
        $map = ['p.pid'=>$pid];
        $result = $this->model->alias('p')->where($map)
        ->join('LEFT JOIN itboye_category_prop as c on c.id=p.prop_id')
        ->field('c.propname,p.value_name') -> select();
        if($result === false){
            return $this -> apiReturnErr($this -> model -> getDbError());
        }
        $arr = [];
        foreach($result as $vo){
            $arr[] = ['name'=>$vo['propname'],'value'=>$vo['value_name']];
        }
        return $this -> apiReturnSuc($arr);
    }
    /**
     * 返回json格式
     * @param $pid
     */
    public function getPropByPid($pid){
        $map = [
            'pid' => $pid,
        ];
        $result = $this -> model -> where($map) -> field('prop_id,id') -> select();
        if($result === false){
            return $this -> apiReturnErr($this -> model -> getDbError());
        }
        $arr = [];
        foreach($result as $vo){
            array_push($arr,['id'=>$vo['prop_id'],'vid'=>$vo['id']]);
        }
        return $this -> apiReturnSuc(json_encode($arr));
    }

}